package ge.battlecity;

import java.io.IOException;
import java.util.Hashtable;

import javax.websocket.EndpointConfig;
import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.RemoteEndpoint.Basic;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;

@ServerEndpoint("/websocket")
public class websoket {

	private static Hashtable<String, ServerThread> game = new Hashtable<>();

	ServerThread server = null;
	Boolean connected = false;

	@OnOpen
	public void onOpen(Session session, EndpointConfig config)
			throws IOException {

	}
	
	@OnMessage
	public void onMessage(String message, Session session) {
		Basic basic = session.getBasicRemote();
		System.out.println(game.size());

		ServerThread server = game.get(message);

		if (server == null) {
			
			server = new ServerThread(basic);
			this.server = server;
			
			new Thread(server).start();
			game.put(message, server);
			this.connected = true;
			
		} else {
			server.addPlayer(basic);
		}
		
	}

	@OnError
	public void onError(Throwable t) {
		t.printStackTrace();
	}

	@OnClose
	public void onClose(Session session) {

	}
}